// 最大岛屿
var numIslands = function (grid) {
  const cLen = grid.length;
  const rLen = grid[0].length;
  let res = 0;
  function dfs(r, c) {
    if (r < 0 || c < 0 || r >= rLen || c >= cLen || grid[c][r] === "0") {
      return;
    }
    grid[c][r] = "0";
    dfs(r, c - 1);
    dfs(r - 1, c);
    dfs(r + 1, c);
    dfs(r, c + 1);
  }
  for (let c = 0; c < cLen; c++) {
    for (let r = 0; r < rLen; r++) {
      if (grid[c][r] === "1") {
        res++;
        dfs(r, c);
      }
    }
  }
  return res;
};

console.log(
  numIslands([
    ["1", "1", "1", "1", "0"],
    ["1", "1", "0", "1", "0"],
    ["1", "1", "0", "0", "0"],
    ["0", "0", "0", "0", "0"],
  ])
);
